home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / language / embedded / mcu / asembler.arc / DO4.C < prev    next >
Text File  |  1987-12-09  |  4KB  |  188 lines

  1. /*
  2.  *      MC6804 specific processing
  3.  */
  4.  
  5. #define IMMED   0
  6. #define IND     1
  7. #define OTHER   2
  8.  
  9. /* special addresses */
  10. #define XREG    0x80
  11. #define YREG    0x81
  12. #define SD1REG  0x82
  13. #define SD2REG  0x83
  14. #define ACCUM   0xFF
  15.  
  16. /*
  17.  *      localinit --- machine specific initialization
  18.  */
  19. localinit()
  20. {
  21.     install("x",XREG);
  22.     install("X",XREG);
  23.     install("y",YREG);
  24.     install("Y",YREG);
  25.     install("a",ACCUM);
  26.     install("A",ACCUM);
  27. }
  28.  
  29. /*
  30.  *      do_op --- process mnemonic
  31.  */
  32. do_op(opcode,class)
  33. {
  34.     int     dist;   /* relative branch distance */
  35.     int     amode;  /* indicated addressing mode */
  36.     int     r1;     /* first eval() for mvi */
  37.  
  38.     if (( *Operand == '[' ) || ( *Operand == ','))
  39.         amode = IND;
  40.     else if( *Operand == '#' )
  41.         amode = IMMED;
  42.     else
  43.         amode = OTHER;
  44.  
  45.     switch(class){
  46.         case INH:                       /* inherent addressing */
  47.             emit(opcode);
  48.             return;
  49.         case APOST:             /* A address in mem follows opcode */
  50.             emit(opcode);
  51.             emit(ACCUM);
  52.             return;
  53.         case REL:                       /* short relative branches */
  54.             eval();
  55.             dist = Result - (Pc+1);
  56.             if( (dist >15 || dist <-16) && Pass==2){
  57.                 error("Branch out of Range");
  58.                 dist = -1;
  59.                 }
  60.             emit(opcode + (dist&0x1F));
  61.             return;
  62.         case BTB:
  63.         case SETCLR:
  64.             eval();
  65.             if(Result <0 || Result >7){
  66.                 error("Bit Number must be 0-7");
  67.                 return;
  68.                 }
  69.             emit( opcode + Result);
  70.             if(*Optr++ != ',')error("SYNTAX");
  71.             eval();
  72.             emit(lobyte(Result));
  73.             if( class == SETCLR )
  74.                 return;
  75.             if(*Optr++ != ',')error("SYNTAX");
  76.             eval();
  77.             dist = Result - (Old_pc+3);
  78.             if( (dist >127 || dist <-128) && Pass==2){
  79.                 error("Branch out of Range");
  80.                 dist = -3;
  81.                 return;
  82.                 }
  83.             emit(lobyte(dist));
  84.             return;
  85.         case EXT:               /* jsr, jmp */
  86.             eval();
  87.             emit(opcode | (hibyte(Result) & 0x0F));
  88.             emit(lobyte(Result));
  89.             return;
  90.         case BPM:       /* brset/clr 7,accum,target */
  91.             emit(opcode);
  92.             emit(ACCUM);
  93.             eval();
  94.             dist = Result - (Old_pc + 3);
  95.             if ((dist > 127 || dist < -128) && Pass == 2) {
  96.                 error("Branch out of range");
  97.                 dist = -3;
  98.                 return;
  99.                 }
  100.             emit(lobyte(dist));
  101.             return;
  102.         case MVI:
  103.             eval();
  104.             r1 = Result;    /* save result */
  105.             if (*Optr++ != ',')
  106.                 warn("Missing ','");
  107.             eval();
  108.             mvi(opcode,r1,Result);
  109.             return;
  110.         case CLRX:      /* mvi xreg,0 */
  111.             mvi(opcode,XREG,0);
  112.             return;
  113.         case CLRY:      /* mvi yreg,0 */
  114.             mvi(opcode,YREG,0);
  115.             return;
  116.         case LDX:       /* mvi xreg data */
  117.             if (amode == IMMED) Optr++;
  118.             eval();
  119.             mvi(opcode,XREG,Result);
  120.             return;
  121.         case LDY:       /* mvi yreg data */
  122.             if (amode == IMMED) Optr++;
  123.             eval();
  124.             mvi(opcode,YREG,Result);
  125.             return;
  126.         case NOIMM:
  127.             if( amode == IMMED ){
  128.                 error("Immediate Addressing Illegal");
  129.                 return;
  130.                 }
  131.         case GEN:
  132.             if ( amode == IMMED ) {
  133.                 Optr++;
  134.                 eval();
  135.                 emit(opcode | 0x08);
  136.                 emit(Result);
  137.                 return;
  138.                 }
  139.             if( amode == IND ){
  140.                 Optr++;
  141.                 eval();
  142.                 if ((*Optr != ']') && (*Operand != ','))
  143.                     warn("Missing ']'");
  144.                 if (Result != XREG && Result != YREG) {
  145.                     error("Operand must be $80 or $81");
  146.                     emit(opcode);
  147.                     return;
  148.                 }
  149.                 emit(opcode | ((Result&0x01)<<4));
  150.                 return;
  151.                 }
  152.             eval();
  153.             if (XREG <= Result && Result <=SD2REG){
  154.                 /*check for short direct cases*/
  155.                 if ( opcode==0xE6 ) {   /* inc */
  156.                     emit(0xA8 + (Result-XREG));
  157.                     return;
  158.                     }
  159.                 if ( opcode==0xE7 ) { /* dec */
  160.                     emit(0xB8 + (Result-XREG));
  161.                     return;
  162.                     }
  163.                 if ( opcode==0xE0 ) { /* lda */
  164.                     emit(0xAC | (Result-XREG));
  165.                     return;
  166.                     }
  167.                 if ( opcode==0xE1 ) { /* sta */
  168.                     emit(0xBC | (Result-XREG));
  169.                     return;
  170.                     }
  171.                 }
  172.             /* else direct addressing */
  173.             emit( opcode | 0x18);
  174.             emit(lobyte(Result));
  175.             return;
  176.         default:
  177.             fatal("Error in Mnemonic table");
  178.         }
  179. }
  180.  
  181. mvi(op,to,from)
  182. int op,to,from;
  183. {
  184.     emit(op);
  185.     emit(to);
  186.     emit(from);
  187. }
  188.